package com.zsrd.lessons.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zsrd.lessons.domain.TsLessons;
import com.zsrd.lessons.domain.dto.TsLessonsAddDto;
import com.zsrd.lessons.domain.dto.TsLessonsListDto;
import com.zsrd.lessons.domain.dto.TsLessonsUpdateDto;
import com.zsrd.lessons.mapper.TsLessonsMapper;
import com.zsrd.lessons.service.TsLessonsHistoryService;
import com.zsrd.lessons.service.TsLessonsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程表 服务实现类
 * </p>
 *
 * @author Chen Wei
 * @since 2022-07-11
 */
@Service
public class TsLessonsServiceImpl extends ServiceImpl<TsLessonsMapper, TsLessons> implements TsLessonsService {
//    @Autowired
//    private SysFileService sysFileService;
    @Autowired
    private TsLessonsHistoryService tsLessonsHistoryService;

    @Override
    public void updateViews(Long id) {
        this.baseMapper.updateViews(id);
    }

    @Override
    public IPage<TsLessonsListDto> getLessonList(String keyword, String orgNo, Long current, Long size) {
        Page<TsLessonsListDto> page = new Page<>(current, size);
        IPage<TsLessonsListDto> iPage = this.baseMapper.getLessonList(page, keyword, orgNo);
        return iPage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addLesson(TsLessonsAddDto tsLessons) {
        QueryWrapper<TsLessons> eq = new QueryWrapper<TsLessons>().eq("org_no", tsLessons.getOrgNo())
                .eq("title", tsLessons.getTitle());
        int count = count(eq);
        if (count > 0) {
            throw new RuntimeException("标题重复");
        }
        TsLessons bean = new TsLessons();
        BeanUtils.copyProperties(tsLessons, bean);
        bean.setUpdateTime(LocalDateTime.now());
        bean.setCreateTime(LocalDateTime.now());
        bean.setViews(0);
        save(bean);
//        if (tsLessons.getFileId() != null) {
//            SysFile sysFile = new SysFile();
//            sysFile.setId(tsLessons.getFileId());
//            sysFile.setReleType("ts_lessons");
//            sysFile.setReleId(bean.getId().toString());
//            sysFile.setIsDelete(0);
//            sysFileService.updateById(sysFile);
//        }
    }

    @Override
    public void updateLesson(TsLessonsUpdateDto tsLessons) {
        QueryWrapper<TsLessons> eq = new QueryWrapper<TsLessons>().eq("org_no", tsLessons.getOrgNo())
                .eq("title", tsLessons.getTitle()).ne("id", tsLessons.getId());
        int count = count(eq);
        if (count > 0) {
            throw new RuntimeException("标题重复");
        }
        TsLessons bean = new TsLessons();
        BeanUtils.copyProperties(tsLessons, bean);
        bean.setUpdateTime(LocalDateTime.now());
        bean.setCreateTime(LocalDateTime.now());
        updateById(bean);
//        if (tsLessons.getFileId() != null) {
//            SysFile sysFile = new SysFile();
//            sysFile.setId(tsLessons.getFileId());
//            sysFile.setReleType("ts_lessons");
//            sysFile.setReleId(bean.getId().toString());
//            sysFile.setIsDelete(0);
//            sysFileService.updateById(sysFile);
//        }
    }

    @Override
    public Map<String, Object> getLessonById(String loginId, Long id) {
        TsLessons byId = getById(id);
        if (byId == null) {
            throw new RuntimeException("该课程不存在");
        }
        Map<String, Object> m = new HashMap<>();
        m.put("detail", byId);
//        if ("02".equals(byId.getType()) || "03".equals(byId.getType())) {
//            List<SysFile> list = sysFileService.list(new QueryWrapper<SysFile>()
//                    .eq("RELE_TYPE", "ts_lessons")
//                    .eq("RELE_ID", byId.getId())
//                    .eq("IS_DELETE",0)
//                    .orderByDesc("CREATE_TIME"));
//            if (list != null && list.size() > 0) {
//                SysFile sysFile = list.get(0);
//                if ("01".equals(sysFile.getFileType()) || "02".equals(sysFile.getFileType())) {
//                    byte[] buf = sysFile.getFileData();
//                    if (buf != null) {
//                        String fileJson = Base64Utils.encode(buf);
//                        sysFile.setFileJson(fileJson);
//                        sysFile.setFileData(null);
//                    }
//                }
//                m.put("file", sysFile);
//            }
//        }
        //增加阅读历史
        tsLessonsHistoryService.readLessons(loginId, id);
        return m;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteLesson(Long id) {
        removeById(id);
//        SysFile sysFile = new SysFile();
//        sysFile.setIsDelete(1);
//        sysFileService.update(sysFile, new QueryWrapper<SysFile>().eq("RELE_TYPE", "ts_lessons")
//                .eq("RELE_ID", id).eq("IS_DELETE",0));
    }


}
